package com.icim.history;

import static com.icim.define.CommDefine.INITIAL_RETURN_CODE;
import static com.icim.define.CommDefine.XML_NODE_OPE_TBL_NAME;
import static com.icim.define.CommDefine._NORMAL;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.icim.exception.LoadMainException;
import com.icim.rabbitmq.util.RabbitmqManager;
import com.icim.service.impl.LoadMainService;
import com.icim.service.impl.ShowStatusService;
import com.icim.tx.DealErrorQueuePo.DealErrorQueuePoOA;
import com.mysql.jdbc.Util;

public class ErrorQueueHandler {

	private Logger logger = Logger.getLogger(ErrorQueueHandler.class);
	@Resource
	RabbitmqManager rabbitmqManager;

	@Resource
	LoadMainService loadErrorMainService;

	@Resource
	ShowStatusService showStatusService;

	private String commonErrorQueue;
	private String retPrdInfoErrorQueue;
	private String spcRetMfgErrorEueue;
	private String retToolStErrorQueue;
	private String retPnlDefErrorQueue;
	private String retWorkPosErrorQueue;
	private String retBoxXfrErrorQueue;
	private String bisBrmErrorQueue;
	private String retTolStcErrorQueue;
	private String retPrdProcErrorQueue;
	private String jrep2ErrorQueue;
	private String retBoxProcErrorQueue;
	private String spcChartDataRowErrorQueue;
	private String spcOtherProcErrorQueue;
	private String retRtdProcErrorQueue;



	@SuppressWarnings("unused")
	private void dealErrorQueue(String queueName) throws IOException {
		StringBuffer messageBuf = new StringBuffer();
		int dealCnt = 0, successCnt = 0, unSuccessCnt;
		long lRc = INITIAL_RETURN_CODE;
		int fetchCnt = 0;

		fetchCnt = showStatusService.getQueueDepth(queueName);

		if (StringUtils.isEmpty(queueName)) {
			return;
		}

		if (fetchCnt == 0) {
			return;
		}

		loadErrorMainService.setHisQueueName(queueName);
		loadErrorMainService.setHisErrorQueueName(queueName);

		for (int i = 0; i < fetchCnt; i++) {

			DealErrorQueuePoOA oary = new DealErrorQueuePoOA();
			messageBuf.setLength(0);
			messageBuf.append(rabbitmqManager.recieveMessage(queueName));
			String message = messageBuf.toString();
			logger.info("deal error queue process:");
			logger.info(message);
//			if (!message.contains(XML_NODE_OPE_TBL_NAME)) {
//				continue;
//			}
			try {

				lRc = loadErrorMainService.SubMainProc(message);

				if (messageBuf.toString() == null) {
					break;
				}

			} catch (LoadMainException ex) {
				logger.error(Util.stackTraceToString(ex));

			} finally {

				oary.setIndex(dealCnt + 1);
				dealCnt++;

				if (lRc == _NORMAL) {
					successCnt++;
				} else {
					rabbitmqManager.setMessage(queueName, messageBuf.toString());
					logger.error("message has bean sent to error queue");
				}

			}
		}
		unSuccessCnt = dealCnt - successCnt;

		logger.info("deal[" + dealCnt + "]queues ,success[" + successCnt + "]" + ",unSuccess[" + unSuccessCnt);

	}

	public void subMainProc() {
		try {
			dealErrorQueue(commonErrorQueue);
			dealErrorQueue(retPrdInfoErrorQueue);
			dealErrorQueue(spcRetMfgErrorEueue);
			dealErrorQueue(retToolStErrorQueue);
			dealErrorQueue(retPnlDefErrorQueue);
			dealErrorQueue(retWorkPosErrorQueue);
			dealErrorQueue(retBoxXfrErrorQueue);
			dealErrorQueue(retTolStcErrorQueue);
			dealErrorQueue(bisBrmErrorQueue);
			dealErrorQueue(retPrdProcErrorQueue);
			dealErrorQueue(jrep2ErrorQueue);
			dealErrorQueue(retBoxProcErrorQueue);
			dealErrorQueue(spcChartDataRowErrorQueue);
			dealErrorQueue(spcOtherProcErrorQueue);
			dealErrorQueue(retRtdProcErrorQueue);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	
	
	
	public String getCommonErrorQueue() {
		return commonErrorQueue;
	}

	public void setCommonErrorQueue(String commonErrorQueue) {
		this.commonErrorQueue = commonErrorQueue;
	}

	public String getRetPrdInfoErrorQueue() {
		return retPrdInfoErrorQueue;
	}

	public void setRetPrdInfoErrorQueue(String retPrdInfoErrorQueue) {
		this.retPrdInfoErrorQueue = retPrdInfoErrorQueue;
	}

	public String getSpcRetMfgErrorEueue() {
		return spcRetMfgErrorEueue;
	}

	public void setSpcRetMfgErrorEueue(String spcRetMfgErrorEueue) {
		this.spcRetMfgErrorEueue = spcRetMfgErrorEueue;
	}

	public String getRetToolStErrorQueue() {
		return retToolStErrorQueue;
	}

	public void setRetToolStErrorQueue(String retToolStErrorQueue) {
		this.retToolStErrorQueue = retToolStErrorQueue;
	}

	public String getRetPnlDefErrorQueue() {
		return retPnlDefErrorQueue;
	}

	public void setRetPnlDefErrorQueue(String retPnlDefErrorQueue) {
		this.retPnlDefErrorQueue = retPnlDefErrorQueue;
	}

	public String getRetWorkPosErrorQueue() {
		return retWorkPosErrorQueue;
	}

	public void setRetWorkPosErrorQueue(String retWorkPosErrorQueue) {
		this.retWorkPosErrorQueue = retWorkPosErrorQueue;
	}

	public String getRetBoxXfrErrorQueue() {
		return retBoxXfrErrorQueue;
	}

	public void setRetBoxXfrErrorQueue(String retBoxXfrErrorQueue) {
		this.retBoxXfrErrorQueue = retBoxXfrErrorQueue;
	}

	public String getBisBrmErrorQueue() {
		return bisBrmErrorQueue;
	}

	public void setBisBrmErrorQueue(String bisBrmErrorQueue) {
		this.bisBrmErrorQueue = bisBrmErrorQueue;
	}

	public String getRetPrdProcErrorQueue() {
		return retPrdProcErrorQueue;
	}

	public void setRetPrdProcErrorQueue(String retPrdProcErrorQueue) {
		this.retPrdProcErrorQueue = retPrdProcErrorQueue;
	}

	public String getJrep2ErrorQueue() {
		return jrep2ErrorQueue;
	}

	public void setJrep2ErrorQueue(String jrep2ErrorQueue) {
		this.jrep2ErrorQueue = jrep2ErrorQueue;
	}

	public String getRetBoxProcErrorQueue() {
		return retBoxProcErrorQueue;
	}

	public void setRetBoxProcErrorQueue(String retBoxProcErrorQueue) {
		this.retBoxProcErrorQueue = retBoxProcErrorQueue;
	}

	public String getSpcChartDataRowErrorQueue() {
		return spcChartDataRowErrorQueue;
	}

	public void setSpcChartDataRowErrorQueue(String spcChartDataRowErrorQueue) {
		this.spcChartDataRowErrorQueue = spcChartDataRowErrorQueue;
	}

	public String getSpcOtherProcErrorQueue() {
		return spcOtherProcErrorQueue;
	}

	public void setSpcOtherProcErrorQueue(String spcOtherProcErrorQueue) {
		this.spcOtherProcErrorQueue = spcOtherProcErrorQueue;
	}

	public String getRetRtdProcErrorQueue() {
		return retRtdProcErrorQueue;
	}

	public void setRetRtdProcErrorQueue(String retRtdProcErrorQueue) {
		this.retRtdProcErrorQueue = retRtdProcErrorQueue;
	}

	public String getRetTolStcErrorQueue() {
		return retTolStcErrorQueue;
	}

	public void setRetTolStcErrorQueue(String retTolStcErrorQueue) {
		this.retTolStcErrorQueue = retTolStcErrorQueue;
	}
}
